package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.redis.RedisTimeConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.saas.enums.UserTeamEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.IndexTypeModel;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2022/04/18 17:24
 */
@Service("indexTypeModel")
public class IndexTypeModelImpl implements IndexTypeModel {

    @Resource
    private CompanyConfigModel companyConfigModel;


    @Override
    public IndexTypeEnum getIndexType(String corpid, Integer businessType, Integer saasMark) throws XbbException {

        if (isSpecialIndex(corpid, businessType, saasMark)) {
            IndexTypeEnum indexTypeEnum = IndexTypeEnum.IDX_SAAS_CUSTOMER_SPECIAL;
            return indexTypeEnum;
        }
        return EsUtil.getIndexTypeEnum(saasMark, businessType);
    }


    @Override
    public IndexTypeEnum getSubIndexType(String corpid, Integer businessType, Integer saasMark) throws XbbException {
        if (isSpecialIndex(corpid, businessType, SaasMarkEnum.SAAS.getCode())) {
            return IndexTypeEnum.IDX_SAAS_CUSTOMER_SUB_SPECIAL;
        }
        IndexTypeEnum indexTypeEnum = IndexTypeEnum.IDX_FORM_DATA;
        if (Objects.equals(SaasMarkEnum.SAAS.getCode(), saasMark)) {
           indexTypeEnum = getByCode(corpid, businessType);
        }
        indexTypeEnum = EsUtil.getSubFormIndexTypeEnum(indexTypeEnum);
        return indexTypeEnum;
    }

    @Override
    public IndexTypeEnum getTeamIndexType(String corpid, Integer businessType) throws XbbException {
        if (isSpecialIndex(corpid, businessType, SaasMarkEnum.SAAS.getCode())) {
            return IndexTypeEnum.IDX_SAAS_CUSTOMER_TEAM_SPECIAL;
        }
        return UserTeamEnum.getByCode(businessType).getIndexTypeEnum();
    }

    private boolean isSpecialIndex(String corpid, Integer businessType, Integer saasMark) {
        String value = companyConfigModel.getByConfigAlias(CompanyConfigEnum.ELASTICSEARCH_INDEX.getAlias(), CompanyConfigEnum.ELASTICSEARCH_INDEX.getAlias(), "0", "0", RedisTimeConstant.TEN_MINUTES);
        Set<String> corpidSet = new HashSet<>();
        if (StringUtil.isNotEmpty(value)) {
            corpidSet = new HashSet<>(JSON.parseArray(value, String.class));
        }
        return corpidSet.contains(corpid) && Objects.equals(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), businessType);
    }


    @Override
    public IndexTypeEnum getCustomerIndex(String corpid, Integer businessType, Integer saasMark) {
        if (isSpecialIndex(corpid, businessType, saasMark)) {
            return IndexTypeEnum.IDX_SAAS_CUSTOMER_SPECIAL;
        }
        return IndexTypeEnum.IDX_SAAS_CUSTOMER;
    }

    @Override
    public IndexTypeEnum getCustomerSubIndex(String corpid, Integer businessType, Integer saasMark) throws XbbException {
        if (isSpecialIndex(corpid, businessType, saasMark)) {
            return IndexTypeEnum.IDX_SAAS_CUSTOMER_SUB_SPECIAL;
        }
        return IndexTypeEnum.IDX_SAAS_CUSTOMER_SUB;
    }

    @Override
    public IndexTypeEnum getCustomerTeamIndex(String corpid, Integer businessType, Integer saasMark)  {
        if (isSpecialIndex(corpid, businessType, saasMark)) {
            return IndexTypeEnum.IDX_SAAS_CUSTOMER_TEAM_SPECIAL;
        }
        return IndexTypeEnum.IDX_SAAS_CUSTOMER_TEAM;
    }

    @Override
    public IndexTypeEnum getByCode(String corpid, Integer businessType) {
        if (Objects.equals(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), businessType)&& isSpecialIndex(corpid, businessType, SaasMarkEnum.SAAS.getCode())) {
            return getCustomerIndex(corpid, businessType, SaasMarkEnum.SAAS.getCode());
        }
        return IndexTypeEnum.getByCode(businessType);
    }

}
